	<?php
		function whichIsAddingLog($clientID){
			$query = "SELECT ip FROM mod_clientip WHERE pid = '".$clientID."'";
			$rs = mysql_query($query);
			$row = mysql_fetch_array($rs);
			return "<b>".$row[0]."</b>";
		}
		
		function fetchData($resource){
			while($row = mysql_fetch_array($resource)){
				echo html_entity_decode("<span style='color:red; font-size: 12px; font-weight: bold'>".$row['username']." (".$row['timestamps'].")</span>".$row['comments']."<br/>", ENT_QUOTES);
			}
		}
		
		function getLastLogModified(){
			$query = "SELECT ip, tag, username, timestamps FROM ((mod_servermanager A INNER JOIN mod_serverlog B ON A.id = B.hostingid) INNER JOIN  mod_clientip C ON  C.pid = B.clientid) INNER JOIN tbladmins D ON  B.userid = D.id ORDER BY timestamps ASC";
			$rs = mysql_query($query);
			
			$table = "<table border='1'; cellspacing='2' cellpadding='2' width='100%'>
						<tr>
							<th>IP</th>
							<th>Doamin</th>
							<th>Modified by</th>
							<th>Last Modified</th>
							<td></td>
							<td></td>
						</tr>";
			while($row = mysql_fetch_array($rs)){
				$table .= "<tr>
							<td>".$row['ip']."</td>".
							"<td>".$row['tag']."</td>".
							"<td>".$row['username']."</td>".
							"<td>".$row['timestamps']."</td>".
							"<td align='center'>
								<a href=''>View (Add log) </a>							
							</td>
							<td align='center'>
								<a href=''>Delete</a>
							</td>
							</tr>";			
			}
				return $table .= "</table>";	
		}
		
		function getClientIPS($hostid){
			$query = "SELECT pid, ip, comment, hostingip, customer FROM mod_clientip A INNER JOIN mod_servermanager B ON A.hostingip = B.id WHERE hostingip =".mysql_real_escape_string($hostid)." ORDER BY pid ASC	";
			$rs = mysql_query($query);
			$count = mysql_num_rows($rs);
			
			if($count==0)
				echo $msg = "<center>There are no Client IPs for Server tag <b>'".$tag."'</b></center>";
			else{
				//if current is update state, create a Add new IP link
				$isUpdate = (isset($_GET['act']) && $_GET['act']=='edit')?1:0;								
				if($isUpdate)
					echo "<a href='?act=add&hostid=".mysql_real_escape_string($_GET['hostid'])."'>Add New IP</a>";
					
				$table = "<table border='0' cellpadding='4' cellspacing='2' align='center' width='70%' class='ipInfo'>	
							<tr>
								<th>Client IPs</th>
								<th>Comment</th>			
								<td>&nbsp;</td>								
							</tr>";
				while($row = mysql_fetch_array($rs)){
					$table .= "<tr>
									<td><a href='clientshosting.php?".$row['customer']."'>".$row['ip']."</a></td>".
									"<td width='40%'>".(($row['comment']=="")?"&nbsp;":$row['comment'])."</td>".
									"<td align='center'>
										<a href='?module=server_log&pid=".$row['pid']."&hostid=".$hostid."&act=edit'><img src='../modules/admin/server_log/images/edit-icon.png' alt='Edit' /></a>
										<a href='?module=server_log&pid=".$row['pid']."&hostid=".$hostid."&act=del' onClick='return deleteConfirm()'><img src='../modules/admin/server_log/images/Delete-icon.png' alt='Delete' /></a>										
										<a href='?module=server_log&clientID=".$row['pid']."&hostid=".$hostid."#log' name='comment'><img src='../modules/admin/server_log/images/view-icon.png' alt='View log' /></a>
									</td>
								</tr>";			
				}
				return $table."</table>";					
			}
		}
		
		/*Function get clients ip of server , service for server_log_clientip.php file*/
		function getAllServerCBO(){
			//Hosting commbobox
			$HosttinSelect = "SELECT id,tag FROM mod_servermanager ORDER BY id ASC ";
			$HostingRs = mysql_query($HosttinSelect);
			$HostingCBO = "<select name='cboHosting' onChange='gotoOtherServer(this.value)' />";
			$HostingCBO .= "<option value=''>Please select a server tag</option>";
			while($row = mysql_fetch_array($HostingRs)){
				if($row['id'] == mysql_real_escape_string($_GET['hostid']))
					$HostingCBO .= "<option value='".$row['id']."' selected>".$row['tag']."</option>";
				else
					$HostingCBO .= "<option value='".$row['id']."'>".$row['tag']."</option>";
			}
			return $HostingCBO."</select>";
		}
		
		/*Function get all server to add to log*/
		function getAllServer(){
			//Hosting commbobox
			$HosttinSelect = "SELECT id,tag FROM mod_servermanager ORDER BY id ASC ";
			$HostingRs = mysql_query($HosttinSelect);
			$HostingCBO = "<select name='cboHosting' id='cboHosting' onChange='enableButton()' />";
			$HostingCBO .= "<option value=''>Please select a server tag</option>";
			while($row = mysql_fetch_array($HostingRs)){
				if($row['id'] == mysql_real_escape_string($_GET['hostid']))
					$HostingCBO .= "<option value='".$row['id']."' selected>".$row['tag']."</option>";
				else
					$HostingCBO .= "<option value='".$row['id']."'>".$row['tag']."</option>";
			}
			return $HostingCBO."</select>";
		}
		
		function isEixtedLog($ip){
			$query = "SELECT count(*) FROM mod_serverlog WHERE ip like '".mysql_real_escape_string($ip)."'";
			$rs = mysql_query($query);
			$row = mysql_fetch_array($rs);
			return ($row[0]==0)?false:true;
		}
		
		function getServerLogModified(){
			$query = "SELECT  sid, ip, domain, username, timestamps FROM mod_serverlog A INNER JOIN tbladmins B ON A.createdby = B.id GROUP BY ip ORDER BY timestamps  ASC";
			$rs = mysql_query($query);
			$count = mysql_num_rows($rs);
			
			$table = "<div class='tablebg'>
								<table class='datatable' width='100%' border='0' cellspacing='1' cellpadding='3'>
						<tr>
							<th>IP</th>
							<th>Doamin</th>
							<th>Created by</th>
							<th>Last Modified</th>
							<td></td>
							<td></td>
						</tr>";
			if($count != 0){
				while($row = mysql_fetch_array($rs)){
					$linkIP = (isHostingActive($row['ip']))?"<a href=''>".$row['ip']."</a>":$row['ip'];
					$table .= "<tr>
								<td>".$linkIP."</td>".
								"<td>".$row['domain']."</td>".
								"<td>".$row['username']."</td>".
								"<td>".$row['timestamps']."</td>".
								"<td align='center'>
									<a href='?module=server_log&serverID=".$row['sid']."&act=add#log'> View/Add logs	 </a>							
								</td>
								<td align='center'>
									<a href='?module=server_log&serverID=".$row['ip']."&act=del' onClick='return deleteConfirm()' >Delete</a>
								</td>
								</tr>";			
				}
			}else
				$table .= "<tr>
								<td colspan='6' align='center'><i>No record found</i></td>
							</tr>";
			
				return $table .= "</table></div>";	
		}
		
		function fetchServerLog($id){
			$query = "SELECT ip FROM mod_serverlog WHERE sid = '".mysql_real_escape_string($id)."' OR ip = '".mysql_real_escape_string($id)."'";
			$rs = mysql_query($query);
			$row = mysql_fetch_array($rs);
			
			$query = "SELECT sid, username, timestamps,comments FROM mod_serverlog A INNER JOIN tbladmins B ON A.modifiedby = B.id WHERE ip = '".$row['ip']. "' AND comments is not null";
			$resource = mysql_query($query);
			$count = mysql_num_rows($resource);
			if($count!=0)
				while($row = mysql_fetch_array($resource)){
					echo html_entity_decode("<span style='color:red; font-size: 12px; font-weight: bold'>".$row['username']." (".$row['timestamps'].")</span>".$row['comments']."<br/>", ENT_QUOTES);
				}
			else
				echo "<i>No Log</i>";
		}
		
		function fetchServerLogByTable($id){
			$query = "SELECT ip FROM mod_serverlog WHERE sid = '".mysql_real_escape_string($id)."' OR ip = '".($id)."' ORDER BY timestamps";
			$rs = mysql_query($query);
			$row = mysql_fetch_array($rs);
			
			$query = "SELECT sid, username, timestamps,comments FROM mod_serverlog A INNER JOIN tbladmins B ON A.modifiedby = B.id WHERE ip = '".$row['ip']. "' AND comments is not null";
			$resource = mysql_query($query);
			$count = mysql_num_rows($resource);
			$table = "<table border='0' width='100%' cellspacing='0' cellpadding='5' bgcolor='#f4f4f4' align='center' class='logTable' >";
			
				while($row = mysql_fetch_array($resource)){
					//echo html_entity_decode("<span style='color:red; font-size: 12px; font-weight: bold'>".$row['username']." (".$row['timestamps'].")</span>".$row['comments']."<br/>", ENT_QUOTES);
					$table .= "<tr> 
									<td width='25%' rowspan='2' valign='top' class='username'> ".$row['username']."</td>
									<td width='75%'>".$row['timestamps']."</td>
								</tr>";
					$table .= "<tr>
									<td>".html_entity_decode($row['comments'],ENT_QUOTES)."</td>
								</tr>";
				}
				echo $table .= "</table";			
		}
		
		function whichServerIsAddingLog($id){
			$query = "SELECT ip FROM mod_serverlog WHERE sid = '".mysql_real_escape_string($id)."' OR ip = '".($id)."'";
			$rs = mysql_query($query);
			$row = mysql_fetch_array($rs);
			return "<b>".$row[0]."</b>";
		}
		
		function isLogged($id){
			$query = "SELECT sid FROM mod_serverlog WHERE sid = '".mysql_real_escape_string($id)."' OR ip ='".mysql_real_escape_string($id)."'";
			$rs = mysql_query($query);
			$count = mysql_num_rows($rs);
			return ($count==0)?false:true;
		}
		
		function isHostingActive($hostIP){
			$query = "SELECT id FROM  tblhosting A INNER JOIN mod_serverlog B ON B.ip = A.dedicatedip  WHERE  domainstatus= 'Active'";
			$rs = mysql_query($query);
			$count = mysql_num_rows($rs);
			return $count==0?false:true;		
		}

	?>

